package com.comparator;

import com.entity.Address;
import com.entity.WareHouse;
import java.util.Comparator;


public class CompareAddress implements Comparator<WareHouse> {

    private Address address;

    public CompareAddress(Address ad){
        this.address=ad;
    }

    @Override
    public int compare(WareHouse w1,WareHouse w2){
        float d1=distance(w1.getAddress(),this.address);
        float d2=distance(w2.getAddress(),this.address);
        if(d1<d2)
            return 1;

        return -1;
    }

    public float distance(Address address1,Address address2){
        float betaX=address1.getX()-address2.getX();
        float betaY=address1.getY()-address2.getY();
        return (float)Math.pow(betaX*betaX+betaY*betaY,0.5);
    }
}
